package com.zhsj.OkeHomeConsumption.ui.activity.bussiness;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.zhsj.OkeHomeConsumption.R;
import com.zhsj.OkeHomeConsumption.common.Constants;
import com.zhsj.OkeHomeConsumption.common.MyLazyFragment;
import com.zhsj.OkeHomeConsumption.common.OkeHomeConsumptionApi;
import com.zhsj.OkeHomeConsumption.empty.BusinessBean;
import com.zhsj.OkeHomeConsumption.empty.FormalUserInfo;
import com.zhsj.OkeHomeConsumption.event.LoginUserpanterEvent;
import com.zhsj.OkeHomeConsumption.other.AbastractDragFloatActionButton;
import com.zhsj.OkeHomeConsumption.ui.activity.HomeActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.WebActivity;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.BusinessUIInterFace;
import com.zhsj.OkeHomeConsumption.ui.activity.bussiness.mvp.BussinessPresenter;
import com.zhsj.OkeHomeConsumption.ui.activity.wallet.CityWalletActivity;
import com.zhsj.OkeHomeConsumption.utils.JsonUtils;
import com.zhsj.OkeHomeConsumption.utils.SharedPreferanceUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.zhsj.OkeHomeConsumption.common.Constants.SHARED_PERFERENCE_USERINFO;

/**
 * 商家首页  maintain
 */
public class BusinessActivity extends MyLazyFragment<HomeActivity> implements View.OnClickListener, BusinessUIInterFace {


    @BindView(R.id.button_button)
    AbastractDragFloatActionButton buttonButton;
    Unbinder unbinder;
    @BindView(R.id.zhongshouru_lin)
    LinearLayout zhongshouruLin;
    @BindView(R.id.yitixian_lin)
    LinearLayout yitixianLin;
    @BindView(R.id.yurishouru_lin)
    LinearLayout yurishouruLin;
    @BindView(R.id.ketixian_lin)
    LinearLayout ketixianLin;
    private ImageView back;
    private ImageView option;
    private TextView income;
    private TextView business_payee;
    private TextView business_lookpayee;
    private TextView business_icome;
    private TextView business_alreadly_despoit;
    private TextView business_balance;
    private TextView business_despoit;
    private LinearLayout business_maintain;
    private LinearLayout business_collect_QRcode;
    private LinearLayout business_invite_QRcode;
    private LinearLayout business_store_manger;
    private LinearLayout business_table_manger;
    private LinearLayout business_evalute_manger;
    private ImageView business_member;
    private ImageView business_inviteclient;
    private ImageView business_system;
    private BussinessPresenter minePersonPresenter;
    private SharedPreferanceUtils mSharePreferanceUtils = null;
    private int userid;
    private ArrayList<BusinessBean> businessBeans;
    private FormalUserInfo userInfo = null;
    private AbastractDragFloatActionButton button_button;

    public static BusinessActivity newInstance() {
        return new BusinessActivity();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_business;
    }

    @Override
    protected int getTitleId() {
        return 0;
    }

    @Override
    protected void initView() {

        minePersonPresenter = new BussinessPresenter(getActivity(), this);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(this);
        option = (ImageView) findViewById(R.id.option);
        option.setOnClickListener(this);
        income = (TextView) findViewById(R.id.income);
        income.setOnClickListener(this);
        business_payee = (TextView) findViewById(R.id.business_payee);
        business_payee.setOnClickListener(this);
        business_lookpayee = (TextView) findViewById(R.id.business_lookpayee);
        business_lookpayee.setOnClickListener(this);
        business_icome = (TextView) findViewById(R.id.business_icome);
        business_icome.setOnClickListener(this);
        business_alreadly_despoit = (TextView) findViewById(R.id.business_alreadly_despoit);
        business_alreadly_despoit.setOnClickListener(this);
        business_balance = (TextView) findViewById(R.id.business_balance);
        business_balance.setOnClickListener(this);
        business_despoit = (TextView) findViewById(R.id.business_despoit);
        business_despoit.setOnClickListener(this);
        business_maintain = (LinearLayout) findViewById(R.id.business_maintain);
        business_maintain.setOnClickListener(this);
        business_collect_QRcode = (LinearLayout) findViewById(R.id.business_collect_QRcode);
        business_collect_QRcode.setOnClickListener(this);
        business_invite_QRcode = (LinearLayout) findViewById(R.id.business_invite_QRcode);
        business_invite_QRcode.setOnClickListener(this);
        business_store_manger = (LinearLayout) findViewById(R.id.business_store_manger);
        business_store_manger.setOnClickListener(this);
        business_table_manger = (LinearLayout) findViewById(R.id.business_table_manger);
        business_table_manger.setOnClickListener(this);
        business_evalute_manger = (LinearLayout) findViewById(R.id.business_evalute_manger);
        business_evalute_manger.setOnClickListener(this);
        business_system = (ImageView) findViewById(R.id.business_system);
        business_system.setOnClickListener(this);
        business_member = (ImageView) findViewById(R.id.business_member);
        business_member.setOnClickListener(this);
        business_inviteclient = (ImageView) findViewById(R.id.business_inviteclient);
        business_inviteclient.setOnClickListener(this);
        buttonButton.setVisibility(View.GONE);
    }


    @Override
    protected void initData() {
        mSharePreferanceUtils = new SharedPreferanceUtils(getActivity());
        userInfo = Hawk.get(Constants.SHARED_PERFERENCE_USERINFO);
        userid = userInfo.getId();
        minePersonPresenter.TotalrevenueGet(userid);
    }
    @OnClick({R.id.zhongshouru_lin, R.id.yitixian_lin, R.id.yurishouru_lin, R.id.ketixian_lin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.zhongshouru_lin:
                //总收入
                Intent intent7 = new Intent(getActivity(), BussinessLookpayeeActivity.class);
                intent7.putExtra("flag", 7);//商家 总收入
                startActivity(intent7);
                break;
            case R.id.yitixian_lin:
                Intent intent9 = new Intent(getActivity(), BussinessLookpayeeActivity.class);
                intent9.putExtra("flag", 9);//商家 已提现
                startActivity(intent9);
                break;
            case R.id.yurishouru_lin:
                Intent intent8 = new Intent(getActivity(), BussinessLookpayeeActivity.class);
                intent8.putExtra("flag", 8);//商家 预计收入
                startActivity(intent8);
                break;
            case R.id.ketixian_lin:
                Intent intent5 = new Intent(getActivity(), BusinessWalletActivity.class);
                startActivity(intent5); //可提现金额
                break;
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.business_maintain:
                //商家管理
                startActivity(new Intent(getActivity(), BusinessmaintainActivity.class));
                break;
            case R.id.business_collect_QRcode:
                //收款二维码
                startActivity(ReceiptQRcodeActivity.class);
                break;
            case R.id.business_invite_QRcode:
                //邀请二维码

                Intent intent7 = new Intent(getActivity(), InviteQRcodeActivity.class)
                        .putExtra("flage", 1);
                startActivity(intent7);
                break;
            case R.id.business_lookpayee:
                Intent intent6 = new Intent(getActivity(), BussinessLookpayeeActivity.class);
                intent6.putExtra("flag", 6);//商家 收入记录
                startActivity(intent6);
                break;
            case R.id.back:
                EventBus.getDefault().post(new LoginUserpanterEvent(0));
                break;
            case R.id.business_store_manger:
//                Intent intent10 = new Intent(getActivity(), StoreManagerActivity.class);;
//                startActivity(intent10);
                break;

            //规范制度
            case R.id.business_system:
                Intent intent = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url", OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/contract/4.html");
                startActivity(intent);
                break;
            //邀请客户
            case R.id.business_inviteclient:
                Intent intent3 = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url", OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/platform/13.html");
                startActivity(intent3);
                break;
            //邀请会员
            case R.id.business_member:
                Intent intent4 = new Intent(getActivity(), WebActivity.class)
                        .putExtra("url", OkeHomeConsumptionApi.ELVDOU_BASEURL_H5_LIFE + "app/h5/platform/12.html");
                startActivity(intent4);
                break;


        }

    }

    /**
     * 获取商家总收入
     */
    @Override
    public void TotalrevenueGet(String context) {
        try {
            JSONTokener jsonT = new JSONTokener(context);
            JSONObject jsonOBJ = (JSONObject) jsonT.nextValue();
            String code = jsonOBJ.optString("code");
            String message = jsonOBJ.optString("message");
            if (code.equals("N000000")) {
                JSONObject jsondata = jsonOBJ.optJSONObject("data");

                BusinessBean remake = JsonUtils.object(jsondata.toString(), BusinessBean.class);
                business_icome.setText(remake.getTotalIncome() + "");
                business_despoit.setText(remake.getLianLianBalance() + "");
                business_balance.setText(remake.getBalance() + "");
                business_alreadly_despoit.setText(remake.getExtractCash() + "");
                business_payee.setText(remake.getTodaytIncome() + "");
            } else if (code.equals(Constants.STATUSTOKENERROR)) {
                // token过期
                if (!message.equals("null")) {
                    toast(message);
                }
            } else {
                if (!message.equals("null")) {
                    toast(message);
                }
            }
        } catch (JSONException ex) {
            log("获取商家首页--- 总收入");
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
